package cn.studyjams.s1.contest.autohello.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wefika.flowlayout.FlowLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.studyjams.s1.contest.autohello.R;
import cn.studyjams.s1.contest.autohello.model.Contact;
import cn.studyjams.s1.contest.autohello.model.Event;
import cn.studyjams.s1.contest.autohello.model.RepeatMode;

/**
 * Created by liuyang
 * on 2016/11/25.
 */

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolder> {

    private Context context;
    private List<Event> mDataset;

    public EventAdapter(List<Event> myDataset) {
        mDataset = myDataset;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView mDayTV;
        TextView mYearMonthTV;
        public CardView mEventHolderCV;

        TextView mContentTV;
        FlowLayout mReceiversHolder;
        TextView mExecuteTimeTV;
        public SwitchCompat mIsActiveSC;

        ViewHolder(View v) {
            super(v);
            mDayTV = (TextView) v.findViewById(R.id.day);
            mYearMonthTV = (TextView) v.findViewById(R.id.year_month);
            mEventHolderCV = (CardView) v.findViewById(R.id.event_holder);

            mContentTV = (TextView) v.findViewById(R.id.event_content);
            mReceiversHolder = (FlowLayout) v.findViewById(R.id.receivers_holder);
            mExecuteTimeTV = (TextView) v.findViewById(R.id.execute_time);
            mIsActiveSC = (SwitchCompat) v.findViewById(R.id.is_active);
        }
    }

    @Override
    public EventAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.event_item, parent, false);
        context = parent.getContext();
        return new EventAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Event event = mDataset.get(position);

        holder.mContentTV.setText(event.getContent());

        FlowLayout.LayoutParams layoutParams = new FlowLayout.LayoutParams(
                FlowLayout.LayoutParams.WRAP_CONTENT,
                FlowLayout.LayoutParams.WRAP_CONTENT
        );
        layoutParams.setMargins(0, 4, 8, 4);
        for (Contact contact : event.getReceivers()) {
            TextView textView = new TextView(context);
            textView.setLayoutParams(layoutParams);
            textView.setTextColor(ContextCompat.getColor(context, R.color.textOrIcons));

            textView.setPadding(16, 8, 16, 8);
            textView.setBackground(ContextCompat.getDrawable(context, R.drawable.arc_rectangle_black_bg));

            switch (contact.getType()) {
                case 0:
                    textView.setText(contact.getName() + ":" + contact.getPhoneNumber());
                    break;
                case 1:
                    textView.setText(contact.getName() + ":" + contact.getEmailAddress());
                    break;
                default:
                    break;
            }

            holder.mReceiversHolder.addView(textView);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        String dateTimeStr = event.getDateTime();

        Date datetime = null;
        try {
            datetime = sdf.parse(dateTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(datetime);

        String year = new SimpleDateFormat("yyyy", Locale.getDefault()).format(calendar.getTime());
        String month = new SimpleDateFormat("MM", Locale.getDefault()).format(calendar.getTime());
        String day = new SimpleDateFormat("dd", Locale.getDefault()).format(calendar.getTime());
        String hour = new SimpleDateFormat("HH", Locale.getDefault()).format(calendar.getTime());
        String minute = new SimpleDateFormat("mm", Locale.getDefault()).format(calendar.getTime());


        String dayStr = String.format(context.getResources().getString(R.string.event_item_day), day);
        holder.mDayTV.setText(dayStr);
        String yearMonthStr = String.format(context.getResources().getString(R.string.event_item_year_month), month, year);
        holder.mYearMonthTV.setText(yearMonthStr);

        String timeStr = String.format(context.getResources().getString(R.string.default_time_picker), hour, minute);

        RepeatMode repeatMode = event.getRepeatMode();
        String repeatStr = "";
        switch (repeatMode.getRepeatType()) {
            case 0:
                repeatStr += context.getString(R.string.do_not_repeat);
                break;
            case 1:
                repeatStr += context.getString(R.string.repeat_every_day);
                break;
            case 2:
                repeatStr += context.getString(R.string.repeat_every_week);
                String weeksStr = repeatMode.getWhichWeeks();
                String[] whichWeeks = weeksStr.substring(1, weeksStr.length() - 1).split(", ");

                Map<Integer, String> weekMap = new HashMap<>();
                weekMap.put(0, context.getString(R.string.sun));
                weekMap.put(1, context.getString(R.string.mon));
                weekMap.put(2, context.getString(R.string.tue));
                weekMap.put(3, context.getString(R.string.wed));
                weekMap.put(4, context.getString(R.string.thu));
                weekMap.put(5, context.getString(R.string.fri));
                weekMap.put(6, context.getString(R.string.sat));

                weeksStr = "";
                for (int i = 0; i < 7; i++) {
                    if (whichWeeks[i].equals("true")) {
                        weeksStr += weekMap.get(i) + " ";
                    }
                }

                repeatStr += "; " + weeksStr;
                break;
            case 3:
                repeatStr += context.getString(R.string.repeat_every_month);
                if (repeatMode.getSameDayOrSameWeekOfTheMonth() == 0) {
                    repeatStr += "; " + context.getString(R.string.same_day);
                } else if (repeatMode.getSameDayOrSameWeekOfTheMonth() == 1) {
                    repeatStr += ";" + context.getString(R.string.same_week);
                }
                break;
            case 4:
                repeatStr += context.getString(R.string.repeat_every_year);
                break;
        }

        if (repeatMode.getEndCondition() == 1) {
            //直到某一个日期
            repeatStr += "; " + String.format(context.getString(R.string.end_condition_until), repeatMode.getEndDateTime());
        } else if (repeatMode.getEndCondition() == 2) {
            //直到一定次数
            repeatStr += "; " + String.format(context.getString(R.string.end_condition_for), repeatMode.getEndCounts() + "");
        }

        holder.mExecuteTimeTV.setText(timeStr + ";" + repeatStr);
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}
